/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.client;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlException;

import com.guardianapis.api.domain.xmlbeans.Content;
import com.guardianapis.api.domain.xmlbeans.ContentDocument;
import com.guardianapis.api.domain.xmlbeans.Search;
import com.guardianapis.api.domain.xmlbeans.SearchDocument;
import com.guardianapis.api.domain.xmlbeans.Tags;
import com.guardianapis.api.domain.xmlbeans.TagsDocument;
import com.guardianapis.api.exceptions.ContentException;
import com.guardianapis.api.http.CommonsHttpClient;
import com.guardianapis.api.http.GuardianHttpClient;

public class XmlBeansGuardianContentClientImpl extends BaseGuardianContentClient implements XmlBeansGuardianContentClient {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private static Log log = LogFactory.getLog(XmlBeansGuardianContentClientImpl.class);

	/*******************************************************************************
	 * Constructors
	 *******************************************************************************/

	public XmlBeansGuardianContentClientImpl(String pApiKey) {
		super(new CommonsHttpClient(), pApiKey);
	}

	public XmlBeansGuardianContentClientImpl(GuardianHttpClient pHttpClient, String pApiKey) {
		super(pHttpClient, pApiKey);
	}

	public XmlBeansGuardianContentClientImpl(GuardianHttpClient pHttpClient, String pContentAPIBaseUrl, String pApiKey) {
		super(pHttpClient, pApiKey);
		
		this.apiBaseUrl = pContentAPIBaseUrl;
	}

	/*******************************************************************************
	 * Implementing SimpleGuardianContentClient
	 *******************************************************************************/

	public Search search(SearchRequest pSearchRequest) throws ContentException {
		InputStream responseStream = searchRaw(pSearchRequest);

		SearchDocument searchResult;
		try {
			searchResult = SearchDocument.Factory.parse(responseStream);
			log.trace("Created Search Document: "  + searchResult);
		} catch (XmlException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		} catch (IOException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		}
		return searchResult.getSearch();
	}
	
	public Content item(ItemRequest pItemRequest) throws ContentException {
		InputStream responseStream = itemRaw(pItemRequest);
		ContentDocument contentResult;
		
		try {
			contentResult = ContentDocument.Factory.parse(responseStream);
			log.trace("Created Content Document: "  + contentResult);
		} catch (XmlException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		} catch (IOException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		}
		return contentResult.getContent();
	}

	public Tags tags(TagsRequest pTagsRequest) throws ContentException {
		InputStream responseStream = tagsRaw(pTagsRequest);
		TagsDocument tagsResult;
		
		try {
			tagsResult = TagsDocument.Factory.parse(responseStream);
			log.trace("Created Tags Document: "  + tagsResult);
		} catch (XmlException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		} catch (IOException e) {
			throw new ContentException("Error parsing XML response: " + e.getMessage(),e);
		}
		return tagsResult.getTags();
	}
	
}
